package com.example.sls.dao.social;

import com.example.sls.pojo.social.PrivateMessages;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 私信聊天表(PrivateMessages)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:11
 */
public interface PrivateMessagesDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from private_messages where id=#{id}")
  PrivateMessages queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param privateMessages 查询条件
   * @param pageSize        分页对象
   * @param offset          偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from private_messages where 1=1" +
          " and (id=#{privateMessages.id} or (isnull(#{privateMessages.id}) or #{privateMessages.id}=0))" +
          " and (sender_id=#{privateMessages.senderId} or (isnull(#{privateMessages.senderId}) or #{privateMessages.senderId}=0))" +
          " and (receiver_id=#{privateMessages.receiverId} or (isnull(#{privateMessages.receiverId}) or #{privateMessages.receiverId}=0))" +
          " and (message=#{privateMessages.message} or (isnull(#{privateMessages.message}) or #{privateMessages.message}=''))" +
          " and (message_type=#{privateMessages.messageType} or (isnull(#{privateMessages.messageType}) or #{privateMessages.messageType}=''))" +
          " and (created_at=#{privateMessages.createdAt} or (isnull(#{privateMessages.createdAt}) or #{privateMessages.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<PrivateMessages> queryAllByLimit(PrivateMessages privateMessages, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from private_messages")
  long count();

  /**
   * 新增数据
   *
   * @param privateMessages 实例对象
   * @return 影响行数
   */
  @Insert("insert into private_messages (id,sender_id,receiver_id,message,message_type,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{senderId}!=0 and !isnull(#{senderId}),#{senderId},null)," +
          "if(#{receiverId}!=0 and !isnull(#{receiverId}),#{receiverId},null)," +
          "if(#{message}!='' and !isnull(#{message}),#{message},null)," +
          "if(#{messageType}!='' and !isnull(#{messageType}),#{messageType},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(PrivateMessages privateMessages);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param privateMessages 实例对象
   * @return 影响行数
   */
  @Update("update private_messages set " +
          "sender_id=if(isnull(#{senderId}) or #{senderId}=0,sender_id,#{senderId})," +
          "receiver_id=if(isnull(#{receiverId}) or #{receiverId}=0,receiver_id,#{receiverId})," +
          "message=if(isnull(#{message}) or #{message}='',message,#{message})," +
          "message_type=if(isnull(#{messageType}) or #{messageType}='',message_type,#{messageType})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(PrivateMessages privateMessages);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from private_messages where id=#{id}")
  int deleteById(Integer id);
}

